package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.crm.entity.GwecT;
import cn.e4j.modular.crm.mapper.Gwei005Mapper;
import cn.e4j.modular.crm.service.Gwei005Service;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  发货服务实现类
 * </p>
 *
 * @author mengyb
 * @since 2019-05-08
 */
@Service
public class Gwei005ServiceImpl extends ServiceImpl<Gwei005Mapper, GwecT> implements Gwei005Service {

    @Transactional(readOnly = true)
    @Override
    public List<Dto> list_(Dto inDto, Page<Object> page) {

//        return this.baseMapper.list_(inDto, page);
        return InvokeUtil.invokeBeanMethod("gwei005Mapper,list_",inDto,page);
    }

    @Transactional(readOnly = true)
    @Override
    public List<Dto> listCount(Dto inDto) {
        return this.baseMapper.listCount(inDto);
    }


    @Transactional(readOnly = true)
    @Override
    public List<Dto> list(Dto inDto) {

//        return this.baseMapper.list(inDto);
        return InvokeUtil.invokeBeanMethod("gwei005Mapper,list",inDto);
    }

//    @Transactional(readOnly = true)
    @Override
    public List<Dto> listC(Dto inDto) {
//        return this.baseMapper.listC(inDto);
        return InvokeUtil.invokeBeanMethod("gwei005Mapper,listC",inDto);
    }

    @Override
    public List<Dto> listGwec(Dto inDto) {
//        return this.baseMapper.listGwec(inDto);
        return InvokeUtil.invokeBeanMethod("gwei005Mapper,listGwec",inDto);
    }

//    @Transactional(readOnly = true)
//    @DS("slave_1")
//    @Override
//    public Dto getOeb(Dto inDto) {
//        return this.baseMapper.getOeb(inDto);
//    }

    @Transactional(readOnly = true)
    @Override
    public Dto getOeb(Dto inDto) {
        return this.baseMapper.getOeb(inDto);
    }

//    @Transactional(readOnly = true)
//    @DS("slave_1")
//    @Override
//    public Dto getOea(Dto inDto) {
//        return this.baseMapper.getOea(inDto);
//    }

    @Transactional(readOnly = true)
    @Override
    public Dto getOea(Dto inDto) {
        return this.baseMapper.getOea(inDto);
    }


    @Transactional(readOnly = true)
    @Override
    public List<Dto> listOrg_1(Dto inDto) {
//        return this.baseMapper.listOrg_1(inDto);
        return InvokeUtil.invokeBeanMethod("gwei005Mapper,listOrg_1",inDto);
    }


    @Transactional(readOnly = true)
    @Override
    public List<Dto> getMaxDate(Dto inDto) {
        return this.baseMapper.getMaxDate(inDto);
    }

//    @Transactional(readOnly = true)
//    @Override
//    public List<Dto> listOebPage(Dto inDto, Page<Object> page) {
//        return this.baseMapper.listOebPage(inDto, page);
//    }
    @Transactional(readOnly = true)
    @Override
    public List<Dto> listOebPage(Dto inDto, Page<Object> page) {
//        return this.baseMapper.listOebPage(inDto, page);
        return InvokeUtil.invokeBeanMethod("gwei005Mapper,listOebPage",inDto,page);
    }
    @Transactional(readOnly = true)
    @Override
    public Dto listGwcs(Dto inDto) {
        return this.baseMapper.listGwcs(inDto);
    }
    @Transactional(readOnly = true)
    @Override
    public List<Dto> listCount1(Dto inDto) {
        return this.baseMapper.listCount1(inDto);
    }
    @Transactional
    @Override
    public void add(List<GwecT> gwecTList) {
        ShiroUser shiroUser= ShiroKit.getUser();
        for(GwecT gwecT:gwecTList){
            gwecT.setGWECENT(shiroUser.getDBC());
            gwecT.setGWECUSER(shiroUser.getACCOUNT());
            gwecT.setGWECGRUP(shiroUser.getORG_ID());
            gwecT.setGWECORIU(shiroUser.getACCOUNT());
            gwecT.setGWECORIG(shiroUser.getORG_ID());
            gwecT.setGWECTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
            gwecT.setGWECDATE(LocalDate.now());
            gwecT.setGWECACTI("Y");
            gwecT.setGWECCONF("N");
            gwecT.setGWEC016("Y");
            gwecT.setGWECLEGAL(gwecT.getGWECPLANT());
            gwecT.insert();
        }
    }
    @Transactional
    @Override
    public void edit(List<GwecT> gwecTList) {
        ShiroUser shiroUser= ShiroKit.getUser();
        UpdateWrapper<GwecT> gwecTUpdateWrapper=new UpdateWrapper<>();
        gwecTUpdateWrapper.eq("GWECENT",shiroUser.getDBC());
        gwecTUpdateWrapper.eq("GWECDOCNO", gwecTList.get(0).getGWECDOCNO());
        int a=this.baseMapper.delete(gwecTUpdateWrapper);
        if(a!=0){
            for(GwecT gwecT:gwecTList){
                gwecT.setGWECENT(shiroUser.getDBC());
                gwecT.setGWECUSER(shiroUser.getACCOUNT());
                gwecT.setGWECGRUP(shiroUser.getORG_ID());
                gwecT.setGWECORIU(shiroUser.getACCOUNT());
                gwecT.setGWECORIG(shiroUser.getORG_ID());
                gwecT.setGWECTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
                gwecT.setGWECDATE(LocalDate.now());
                gwecT.setGWECACTI("Y");
                gwecT.setGWECCONF("N");
                gwecT.setGWEC016("Y");
                gwecT.insert();
            }
        }

    }
    @Transactional
    @Override
    public void delRow(GwecT gwecT) {
        ShiroUser shiroUser= ShiroKit.getUser();
        UpdateWrapper<GwecT> gwecTUpdateWrapper = new UpdateWrapper<>();
        gwecTUpdateWrapper.eq("GWECENT", shiroUser.getDBC());
        gwecTUpdateWrapper.eq("GWECDOCNO",gwecT.getGWECDOCNO());
        gwecTUpdateWrapper.eq("GWECSEQ", gwecT.getGWECSEQ());
        gwecT.delete(gwecTUpdateWrapper);
    }
    @Transactional
    @Override
    public void delRows(List<GwecT> gwecTList) {
        ShiroUser shiroUser= ShiroKit.getUser();
        for (GwecT gwecT : gwecTList) {
            UpdateWrapper<GwecT> gwecTUpdateWrapper = new UpdateWrapper<>();
            gwecTUpdateWrapper.eq("GWECENT", shiroUser.getDBC());
            gwecTUpdateWrapper.eq("GWECDOCNO",gwecT.getGWECDOCNO());
            gwecTUpdateWrapper.eq("GWECSEQ", gwecT.getGWECSEQ());
            gwecT.delete(gwecTUpdateWrapper);
        }
    }

    @Transactional
    @Override
    public Dto check(List<GwecT> gwecTList) {
        ShiroUser user= ShiroKit.getUser();
        Dto outDto= Dtos.newDto();
        outDto.setAppCode(200);
        for (GwecT gwecT : gwecTList) {
            UpdateWrapper<GwecT> gwecTUpdateWrapper = new UpdateWrapper<>();
            gwecTUpdateWrapper.eq("GWECENT", user.getDBC());
            gwecTUpdateWrapper.eq("GWECDOCNO", gwecT.getGWECDOCNO());
//            gwecTUpdateWrapper.eq("GWECSEQ", gwecT.getGWECSEQ());
            gwecT.setGWECCONF("Y");
            if(!gwecT.update(gwecTUpdateWrapper)){
                outDto.setAppCode(400);
                outDto.setAppMsg("审核失败");
                //手动回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                break;
            }
        }
        return outDto;
    }

    @Transactional
    @Override
    public Dto unCheck(List<GwecT> gwecTList) {
        ShiroUser user= ShiroKit.getUser();
        Dto outDto= Dtos.newDto();
        outDto.setAppCode(200);
        for (GwecT gwecT : gwecTList) {
            UpdateWrapper<GwecT> gwecTUpdateWrapper = new UpdateWrapper<>();
            gwecTUpdateWrapper.eq("GWECENT", user.getDBC());
            gwecTUpdateWrapper.eq("GWECDOCNO", gwecT.getGWECDOCNO());
//            gwecTUpdateWrapper.eq("GWECSEQ", gwecT.getGWECSEQ());
            gwecT.setGWECCONF("N");
            if(!gwecT.update(gwecTUpdateWrapper)){
                outDto.setAppCode(400);
                outDto.setAppMsg("取消审核失败");
                //手动回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                break;
            }
        }
        return outDto;
    }

    @Transactional(readOnly = true)
    @Override
    public List<Dto> getGwecBydoc(Dto inDto){
        return this.baseMapper.getGwecBydoc(inDto);
    }

    @Transactional(readOnly = true)
    public List<Dto> listDelGwec(Dto inDto){
        return this.baseMapper.listDelGwec(inDto);
    }

}
